package com.leviassaf.firstandroidapp;

import com.facebook.model.OpenGraphAction;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.BaseAdapter;

//abstract base class to represent a list item's basic UI and functionality, like handling click events and notifying observers of data changes
public abstract class BaseListElement {
	private Drawable drawableIcon;
	private String strText1;
	private String strText2;

	//field that will provide a request code that is passed to new activities from the list item.
	//This is used when the launched activity returns a result that the list item needs to process. 
	//Although this field is not part of the UI, you're defining it here as it is used in the constructor for the class
	private int intRequestCode;
	
	private BaseAdapter baseAdapter;
	
	
	public void setAdapter(BaseAdapter adapter) {
		this.baseAdapter = adapter;
	}

	public BaseListElement(Drawable icon, String text1, String text2, int requestCode) {
		super();
		this.drawableIcon = icon;
		this.strText1 = text1;
		this.strText2 = text2;
		this.intRequestCode = requestCode;
	}
	
	public int getRequestCode() {
		return intRequestCode;
	}

	public Drawable getIcon() {
		return drawableIcon;
	}

	public void setIcon(Drawable drawableIcon) {
		this.drawableIcon = drawableIcon;
	}

	public String getText1() {
		return strText1;
	}

	public void setText1(String text1) {
		strText1 = text1;
		if (baseAdapter != null){
			baseAdapter.notifyDataSetChanged();
		}
	}
	
	public String getText2() {
		return strText2;
	}
	
	public void setText2(String text2) {
		strText2 = text2;
		if (baseAdapter != null){
			baseAdapter.notifyDataSetChanged();
		}
	}

	//method that subclasses can override to handle click events
	protected abstract View.OnClickListener getOnClickListener();

	//method is used to handle result callbacks from a launched activity
	protected void onActivityResult(Intent data){}

	//method is used to save selected friends from a launched activity
	protected void onSaveInstanceState(Bundle bundle){}

	//method is used to restore selected friends from a saved state
	protected boolean restoreState(Bundle savedState){
		return false;
	}

	//method is used to notify observers of data changes
	protected void notifyDataChanged(){
		baseAdapter.notifyDataSetChanged();
	}

	//a method list items must override to populate the eat action
	protected abstract void populateOGAction(OpenGraphAction action);
}
